package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdMapper;
import com.qf.pojo.DtsAd;
import com.qf.service.DtsAdService;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * projectName:workspace_6
 * author:hyn 推广管理 广告管理
 * time:2021/12/5 15:46
 * description:
 */
@Service
public class DtsAdServiceImpl implements DtsAdService {
    @Autowired
    private DtsAdMapper adMapper;
    //广告列表
    @Override
    public IPage<DtsAd> findAdAllList(String name, String content, Integer page, Integer limit, String sort, String order) {
        IPage<DtsAd> ipage = new Page(page,limit);
        QueryWrapper<DtsAd> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(content)){
            wrapper.like("content",content);
        }
        wrapper.eq("deleted",false);
        if("asc".equals(order)){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }
        IPage<DtsAd> p = adMapper.selectPage(ipage, wrapper);
        return p;
    }
    //广告添加
    @Override
    public Object insert(DtsAd ad) {
        LambdaQueryWrapper<DtsAd> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(DtsAd::getId);
        wrapper.eq(DtsAd::getName,ad.getName());
        if(adMapper.selectOne(wrapper) != null){
            return ResponseUtil.fail(-1,"广告标题已存在");
        }
        ad.setAddTime(new Date());
        ad.setDeleted(false);
        return adMapper.insert(ad) > 0 ? ResponseUtil.ok(ad) : ResponseUtil.fail(-1,"添加失败");
    }
    @Autowired
    private DtsStorageService storageService;
    //广告修改
    @Override
    public Object update(DtsAd ad) {
        DtsAd dtsAd = adMapper.selectById(ad);
        if(ad.getUrl() != null){
            storageService.deleteUrl(ad.getUrl());
        }
        if(!dtsAd.getName().equals(ad.getName())){
            LambdaQueryWrapper<DtsAd> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DtsAd::getName,ad.getName());
            if(adMapper.selectOne(wrapper) != null){
                return ResponseUtil.fail(-1,"广告标题已存在");
            }
        }
        //删除oss图片
        if(ad.getUrl() != null){
            storageService.deleteUrl(ad.getUrl());
        }
        ad.setUpdateTime(new Date());
        ad.setDeleted(false);
        return adMapper.updateById(ad) > 0 ? ResponseUtil.ok(ad) : ResponseUtil.fail(-1,"修改失败");
    }
    //广告删除
    @Override
    public Object delete(DtsAd ad) {
        //删除oss图片
        if(ad.getUrl() != null){
            storageService.deleteUrl(ad.getUrl());
        }
        return adMapper.deleteById(ad) > 0 ? ResponseUtil.ok() : ResponseUtil.fail(-1,"删除失败");
    }
}
